<?php
/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/27
 * Time: 14:29
 */

namespace app\home\common;


class Page
{
    private $current; //当前页
    private $prev;
    private $next;
    private $count;
    private $pageSize;
    private $pages;       //总共有多少页
    private $module;       //模块
    private $controller;  //控制器
    private $action;      //方法
    private $viewpage=3;    //显示多少页
    private $pagrams;    //显示多少页

    public function  __construct($count,$pageSize,$current){
        $request=request()->instance();
        $this->module=$request->module();
        $this->controller=$request->controller();
        $this->action=$request->action();
        $this->count=$count;
        $this->pageSize=$pageSize;
         $this->current=$current;
         $this->count();
        //解决get参数不能传递
        $preg='/\/+/';
        $this->pagrams=[];
        foreach($_GET as $k=>$v){
           if(!preg_match($preg,$k)){
               $this->pagrams[$k]=$v;
           }
        }
    }

    private function count(){
        $this->pages=ceil(($this->count)/($this->pageSize)); //计算总共有多少页
    }

    /**
     * 生成分页样式
     */
    public function show(){
        $current=$this->current;
        $module=$this->module;
        $controller=$this->controller;
        $action=$this->action;
        $mvc=$module.'/'.$controller.'/'.$action;
        $pageleft='<nav aria-label="Page navigation"> <ul class="pagination">';
        $pageright='</ul></nav>';
        $this->pagrams['p']=$current-1;
        $prev=' <li><a href="'.URL("{$mvc}",$this->pagrams).'" aria-label="Previous"><span aria-hidden="true">&laquo;</span> </a></li>';

        if($current >= $this->pages){
            $current= $this->pages;
        }
        $countpage='<li style="line-height: 30px">总共'. $this->pages.'页</li>';

        $this->pagrams['p']=$current;
        $a1='<li  ><a style="background:#EEE" href="'.URL("{$mvc}",$this->pagrams).'">'.$current.'</a></li>';
        $current1=$current+1;

        $this->pagrams['p']=$current1;
        $a2='<li><a href="'.URL("{$mvc}",$this->pagrams).'">'.$current1.'</a></li>';
        $current2=$current1+1;

        $this->pagrams['p']=$current2;
        $a3=' <li><a href="'.URL("{$mvc}",$this->pagrams).'">'.$current2.'</a></li>';

        $this->pagrams['p']=$current1;
        $next='<li><a href="'.URL("{$mvc}",$this->pagrams).'" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
        if($this->pages>1){
            $page=$pageleft.$countpage.$prev.$a1.$a2.$a3.$next;
        }else{
            $page='';
        }
        return $page;
    }
}
?>
